<?php

class user {
	private $globalAccessLevel = -1;
	private $institutionArray;
	private $uID;
	private $user;
	private $pass;
	private $userName;
	private $firstName;
	private $lastName;
	
	const GROUP_ACTIVEONLY = 1;
	const GROUP_ALL = 2;
	const GROUP_INACTIVE = 3;
	const GROUP_ADMINONLY = 4;
	
	private $availableLanguages = "";

	public function auth($u, $p){
		$dbCon = db::singleton();
		$res = $dbCon->oneRow("SELECT uID, userName, firstName, lastName, isGlobalAdmin, active FROM " . SYS_DB_DBNAME . "." . SYS_DB_PREFIX . "user WHERE userName='" . $dbCon->real_escape_string($u) . "' AND password='" . md5($p) . "'");
		
		if(count($res) == 0){
			// Not logged in.
			$this->globalAccessLevel = -1;
			return false;
		} else {
			
			if($res['active'] == 0){
				$this->globalAccessLevel = -1;
				return false;				
			} else {			
				
				$this->globalAccessLevel = $res['isGlobalAdmin'];
				$this->user = $u;
				$this->pass = md5($p);
				$this->firstName = $res['firstName'];
				$this->lastName = $res['lastName'];
				$this->uID = (int)$res['uID'];
				$this->userName = $res['userName'];
				
				$this->institutionArray = $this->getInstitution();
				
				
				// Check to see if the user has a default album
				$lists = images::getUserLists($this);
				if(count($lists) == 0){
					// Create an album.
					images::createImageSet($this, "My Album", "Default album", true);
				}
				
			}
			return true;
		}
	}
	
	public function getShares(){
		$dbCon = db::singleton();
	
		$sql = "SELECT (SELECT COUNT(*) FROM " . SYS_DB_DBNAME .".user_share_chart WHERE uID_from=" . $this->getuID() . ") AS outbox, (SELECT COUNT(*) FROM " . SYS_DB_DBNAME .".user_share_chart WHERE uID_to=" . $this->getuID() . ") AS inbox;";
		return $dbCon->oneRow($sql);
	}
	
	public function getInbox(){
		/*
		SELECT chart.chID, chart.name, chart.description, user.firstName, user.lastName, user_share_chart.uID_to, user_share_chart.uID_from
		FROM user_share_chart
		LEFT JOIN picboard.chart ON user_share_chart.chart_chID=chart.chID
		LEFT JOIN picboard.user ON user_share_chart.uID_from=user.uID
		WHERE uID_to=1		
		*/
		
		$dbCon = db::singleton();
		
		$sql = "SELECT chart.chID, chart.name, chart.description, user.firstName, user.lastName, user_share_chart.uID_to, user_share_chart.uID_from FROM " . SYS_DB_DBNAME . ".user_share_chart LEFT JOIN " . SYS_DB_DBNAME . ".chart ON user_share_chart.chart_chID=chart.chID LEFT JOIN " . SYS_DB_DBNAME . ".user ON user_share_chart.uID_from=user.uID WHERE uID_to=" . $this->getuID() . ";";
		return $dbCon->single($sql);
	}
	
	public function getOutbox(){
		/*
		SELECT chart.chID, chart.name, chart.description, user.firstName, user.lastName, user_share_chart.uID_to, user_share_chart.uID_from
		FROM user_share_chart
		LEFT JOIN picboard.chart ON user_share_chart.chart_chID=chart.chID
		LEFT JOIN picboard.user ON user_share_chart.uID_to=user.uID
		WHERE uID_from=1		
		*/
		
		$dbCon = db::singleton();
		
		$sql = "SELECT chart.chID, chart.name, chart.description, user.firstName, user.lastName, user_share_chart.uID_to, user_share_chart.uID_from FROM " . SYS_DB_DBNAME . ".user_share_chart LEFT JOIN " . SYS_DB_DBNAME . ".chart ON user_share_chart.chart_chID=chart.chID LEFT JOIN " . SYS_DB_DBNAME . ".user ON user_share_chart.uID_to=user.uID WHERE uID_from=" . $this->getuID() . ";";
		return $dbCon->single($sql);	
	}
	
	public function getGroups($limit = 5){
		// Get the set of groups the user is enrolled with
		return group::getUserGroups( (int)$this->uID );
	}
	
	public function getSubscribedGroupIDs($adminOnly = false){
		$sql = "SELECT group_gID FROM " . SYS_DB_DBNAME . "." . SYS_DB_PREFIX . "group_user WHERE active=1 AND user_uID=" . $this->uID;
		
		if($adminOnly) $sql .= " AND isAdmin=1";
		$dbCon = db::singleton();
		
		$result = $dbCon->single($sql);
		
		$subscribed = Array();
		
		foreach($result as $groupID){
			array_push($subscribed, $groupID['group_gID']);
		}
		
		return $subscribed;
	}
	
	public function getManagedGroups($assoc = false){
		
		$sql = "SELECT group_gID, group.name AS name, institution_instID, institution.name as institutionName FROM " . SYS_DB_DBNAME . "." . SYS_DB_PREFIX . "group_user LEFT JOIN " . SYS_DB_DBNAME . ".group ON group_user.group_gID=group.gID LEFT JOIN " . SYS_DB_DBNAME . ".institution ON group.institution_instID=institution.instID WHERE active=1 AND isAdmin=1 AND user_uID=" . $this->uID;
		
		$dbCon = db::singleton();
		
		$result = $dbCon->single($sql);
		
		$subscribed = Array();
		
		foreach($result as $groupID){
			if($assoc){
				array_push($subscribed, array(
					"gID" => (int)$groupID['group_gID'], 
					"groupName" => $groupID['name'], 
					"institutionName" => $groupID['institutionName'],
					"instID" => (int)$groupID['institution_instID'],
					"groupID" => $groupID['institutionName']
				));			
			} else {
				array_push($subscribed, array((int)$groupID['group_gID'], $groupID['name'], (int)$groupID['institution_instID'], $groupID['institutionName']));
			}
		}
		
		return $subscribed;	
	}
	
	public function getAuthorisationRequests(){
		// Get the groups that i'm an admin for.
		$myGroups = $this->getManagedGroups(true);
		
		$pendingAuthRequests = array();
		
		foreach($myGroups as $group){
			$requests = $group;
			$requests['pending'] = group::getPendingRequests($group['gID']);
			if(count($requests['pending']) > 0) array_push($pendingAuthRequests, $requests);
		}
		
		return $pendingAuthRequests;
	}
	
	public function isInGroup($gID, $type = user::GROUP_ACTIVEONLY){
		$dbCon = db::singleton();
		
		switch($type){
			
			case user::GROUP_ACTIVEONLY:
				$sql = "SELECT COUNT(*) AS isInGroup FROM " . SYS_DB_DBNAME . "." . SYS_DB_PREFIX . "group_user WHERE active=1 AND user_uID=" . $this->uID . " AND group_gID=" . $dbCon->real_escape_string($gID);
			break;
			
			case user::GROUP_ALL:
				$sql = "SELECT COUNT(*) AS isInGroup FROM " . SYS_DB_DBNAME . "." . SYS_DB_PREFIX . "group_user WHERE user_uID=" . $this->uID . " AND group_gID=" . $dbCon->real_escape_string($gID);
			break;
			
			case user::GROUP_INACTIVE:
				$sql = "SELECT COUNT(*) AS isInGroup FROM " . SYS_DB_DBNAME . "." . SYS_DB_PREFIX . "group_user WHERE active=0 AND user_uID=" . $this->uID . " AND group_gID=" . $dbCon->real_escape_string($gID);
			break;
			
			case user::GROUP_ADMINONLY:
				$sql = "SELECT COUNT(*) AS isInGroup FROM " . SYS_DB_DBNAME . "." . SYS_DB_PREFIX . "group_user WHERE active=1 AND user_uID=" . $this->uID . " AND group_gID=" . $dbCon->real_escape_string($gID) . " AND isAdmin=1";				
			break;
		}

		$result = $dbCon->oneRow($sql);	
		return (BOOL)$result['isInGroup'];
	}
	
	public function getInstitution(){
		// Get the user's institution(s) (if any)
		$sql = "SELECT " . SYS_DB_PREFIX . "institution.name, institution.description, institution.instID, " . SYS_DB_PREFIX . "institution_user.admin, " . SYS_DB_PREFIX . "institution_user.active FROM " . SYS_DB_DBNAME . "." . SYS_DB_PREFIX . "institution_user LEFT JOIN " . SYS_DB_DBNAME . "." . SYS_DB_PREFIX . "institution ON " . SYS_DB_PREFIX . "institution_user.institution_instID=" . SYS_DB_PREFIX . "institution.instID WHERE user_uID = " . $this->uID;
		$dbCon = db::singleton();
		$result = $dbCon->single($sql);
		$this->institutionArray = $result;
		return $result;
	}
	
	public function rtnInstitution(){
		return $this->getInstitution();
	}
	
	public function getCharts($limit = 5){
		// Pull cID out of database where uID = uID
		$charts = chart::getUserCharts( $this->uID, $limit );
		
		return $charts;
	}
	
	public function getBooks($limit = 5){
		$books = book::getUserBooks( $this->uID, $limit );
		return $books;
	}
	
	/*
	public function getUsername(){
		return $this->user;
	}
	*/
	
	public function getHash(){
		return $this->pass;
	}
	
	public function getAccessLevel(){
		return $this->globalAccessLevel;
	}
	
	public function getuID(){
		if(is_integer($this->uID)) {
			return $this->uID;
		} else {
			throw new PicBoardException("uID is not an integer in user.class");
		}
		
	}
	
	public function getUserName(){
		return $this->userName;
	}
	
	public function getFirstName(){
		return $this->firstName;
	}
	
	public function getLastName(){
		return $this->lastName;
	}
	
	
	public function getAvailableLanguages(){
		if($this->availableLanguages == ""){
			$this->availableLanguages = language::getAvailableLanguages();
			return $this->availableLanguages;
		} else {
			return $this->availableLanguages;
		}
	}

}

?>